package com.xiaoerkeji.eshop;

import android.content.Context;
import android.os.Build;

import com.jakewharton.processphoenix.ProcessPhoenix;
import com.lt.app.AppHelper;
import com.lt.base.BaseApplication;
import com.lt.common.DebugHelper;
import com.lt.common.DeviceUuidFactory;
import com.lt.common.FileHelper;
import com.lt.phone.ScreenHelper;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;
import com.xiaoerkeji.eshop.utils.JsonHelper;

import androidx.multidex.MultiDex;
import xcrash.ICrashCallback;
import xcrash.XCrash;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class xeApp extends BaseApplication
{
    private static xeApp instance;
    public String clientId; //客户端ID
    public String appVer;  //app版本
    public String systemVer; //系统版本
    public String systemInfo; //系统信息详情

    @Override

    public boolean isDebug()
    {
        return BuildConfig.DEBUG;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        instance = this;
        if (isMainProcess())
        {
            //
            clientId = DeviceUuidFactory.getInstance().getDeviceId();
            appVer = AppHelper.getInstance().getAppVersionName() + "-" + AppHelper.getInstance().getAppVersionCode();
            systemVer = String.valueOf(Build.VERSION.SDK_INT);
            Map<String, String> mapSys = new HashMap<>();
            mapSys.put("brand", Build.BRAND);
            mapSys.put("model", Build.MODEL);
            mapSys.put("screenSize", ScreenHelper.getScreenWidth() + ":" + ScreenHelper.getScreenHeight());
            mapSys.put("androidVer", Build.VERSION.RELEASE);
//            mapSys.put("apiLevel", String.valueOf(Build.VERSION.SDK_INT));
            systemInfo = JsonHelper.toJson(mapSys);
            DebugHelper.w(systemInfo);
            // 在调用TBS初始化、创建WebView之前进行如下配置
            HashMap<String, Object> map = new HashMap<>();
            map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
            map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
            QbSdk.initTbsSettings(map);
            QbSdk.initX5Environment(this, tbsCb);
        }
    }

    public static xeApp getInstance()
    {
        return instance;
    }

    private final QbSdk.PreInitCallback tbsCb = new QbSdk.PreInitCallback()
    {
        @Override
        public void onCoreInitFinished()
        {

        }

        @Override
        public void onViewInitFinished(boolean b)
        {
            //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
            xeSetting.getInstance().setX5Load(b);
        }
    };

    public File getLogDir()
    {
//        File dir = FileHelper.getInstance().getSDPath();
//        if (dir == null)
//        {
//            dir = getCacheDir();
//        }
        File dir = new File(getCacheDir(), "log");
        return dir;
//        return getCacheDir();
    }

    @Override
    protected void attachBaseContext(Context base)
    {
        super.attachBaseContext(base);
        MultiDex.install(this);
        XCrash.init(this,
                new XCrash.InitParameters()
                        .setLogDir(getLogDir().toString())
                        .setJavaCallback(crashCallback)
                        .setJavaRethrow(false)
                        .setJavaLogCountMax(20)
                        .setNativeCallback(crashCallback)
                        .setNativeRethrow(false)
                        .setNativeLogCountMax(20)
                        .setAnrCallback(crashCallback)
                        .setAnrRethrow(false)
                        .setAnrLogCountMax(20));
    }

    private final ICrashCallback crashCallback = (logPath, emergency) ->
    {
        DebugHelper.e("logPath->" + logPath + "\r\nemergency->" + emergency);
        showToast("抱歉,程序出现异常,即将重启");
        try
        {
            Thread.sleep(1000);
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        ProcessPhoenix.triggerRebirth(getContext());
    };
}
